//
//  AnalyseViewController.m
//  Shine
//
//  Created by 王斌 on 16/3/9.
//  Copyright © 2016年 王斌. All rights reserved.
//

#import "AnalyseViewController.h"
#import "TitleCollectionViewCell.h"
#import "StepsCollectionViewCell.h"
#import "calorieCollectionViewCell.h"
#import "weightCollectionViewCell.h"
#import "AdvancedCollectionViewCell.h"


@interface AnalyseViewController ()<UICollectionViewDataSource , UICollectionViewDelegate>

///滑动标题数组
@property(nonatomic,retain)NSArray *titleArr;
@property(nonatomic,retain)UICollectionView *titleCollectionView;
@property(nonatomic,retain)UICollectionView *collectionView;
@property(nonatomic,retain)NSIndexPath *indexPath;
@property(nonatomic,assign)NSInteger count;

@end

@implementation AnalyseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationController.navigationBar.translucent = NO;
    
    [self createView];
    
    NSArray *arr = [[CoreDataManager shareInstance]selectSevenDay];
    self.count = 0;
    for (HealthKit *temp in arr) {
        self.count += [temp.count integerValue];
    }

    // Do any additional setup after loading the view from its nib.
}

- (void)createView{

    //滚动标题

    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH_SCREEN /4, 30);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 5, 0, 5);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    self.titleArr = [NSArray array];
    self.titleArr = @[@"步数",@"大卡",@"体重",@"高级"];
    
    self.titleCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, WIDTH_SCREEN /2, WIDTH_SCREEN, 40) collectionViewLayout:flowLayout];
    self.titleCollectionView.backgroundColor = COLORBLACK;
    [self.view addSubview:self.titleCollectionView];
    self.titleCollectionView.delegate = self;
    self.titleCollectionView.dataSource = self;
    self.titleCollectionView.showsHorizontalScrollIndicator = NO;
    self.titleCollectionView.pagingEnabled = YES;
    self.titleCollectionView.bounces = NO;
    self.navigationItem.titleView = self.titleCollectionView;
    self.indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
//    [self.navigationItem.titleView addSubview:self.titleCollectionView];
//    [self.navigationController.navigationBar addSubview:self.titleCollectionView];
    //页面
    UICollectionViewFlowLayout *flowl = [[UICollectionViewFlowLayout alloc]init];
    //item尺寸一个屏幕
    flowl.itemSize = CGSizeMake(WIDTH_SCREEN, HEIGHT_SCREEN);
    //行间距,列间距
    flowl.minimumLineSpacing = 0;
    flowl.minimumInteritemSpacing = 0;
    //与屏幕四周距离
    flowl.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //滚动方式,横向
    flowl.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //创建collectionView
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH_SCREEN, HEIGHT_SCREEN) collectionViewLayout:flowl];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    //以页滑动
    self.collectionView.pagingEnabled = YES;
    //取消弹回效果
    self.collectionView.bounces = NO;
    [self.view addSubview:self.collectionView];
    
    //注册
    [self.titleCollectionView registerClass:[TitleCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    //步数
    [self.collectionView registerClass:[StepsCollectionViewCell class] forCellWithReuseIdentifier:@"StepsCollectionViewCell"];
    //卡路里
    [self.collectionView registerClass:[calorieCollectionViewCell class] forCellWithReuseIdentifier:@"calorieCollectionViewCell"];
    //体重
    [self.collectionView registerClass:[weightCollectionViewCell class] forCellWithReuseIdentifier:@"weightCollectionViewCell"];
    //高级
    [self.collectionView registerClass:[AdvancedCollectionViewCell class] forCellWithReuseIdentifier:@"AdvancedCollectionViewCell"];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == self.titleCollectionView) {
        return self.titleArr.count;
    } else {
        return self.titleArr.count;
    }
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.titleCollectionView) {
        TitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
        cell.titleLabel.text = self.titleArr[indexPath.row];
        cell.titleLabel.tag = indexPath.row + 1000;
        cell.lineView.tag = indexPath.row + 2000;
        if (self.indexPath == indexPath) {
            cell.titleLabel.textColor = COLORYELLO;
//            cell.lineView.backgroundColor = COLORYELLO;
            cell.titleLabel.font = [UIFont systemFontOfSize:17];

        }else{
            cell.titleLabel.textColor = [UIColor colorWithWhite:0.702 alpha:1.000];
            cell.titleLabel.font = [UIFont systemFontOfSize:13];

        }
        return cell;
    }else{
        if (indexPath.row == 0) {
            StepsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StepsCollectionViewCell" forIndexPath:indexPath];
            cell.averageStepsLabel.text = @"平均步数";
            cell.allStepsLabel.text = @"总步数";
            NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
            //每三位一个逗号
            [numberFormatter setPositiveFormat:@"###,###;"];
            NSString *averageNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithInteger:self.count/7]];
            cell.averageStepsNumber.text = averageNumberString;
            
            NSString *allNumberString = [numberFormatter stringFromNumber:[NSNumber numberWithInteger:self.count]];
            cell.allStepsNumber.text = allNumberString;
            return cell;
        }else if (indexPath.row == 1){
            calorieCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"calorieCollectionViewCell" forIndexPath:indexPath];
            cell.averageCalorieLabel.text = @"平均热量";
            cell.allCalorieLabel.text = @"总热量";
            cell.averageCalorieNumber.text = [NSString stringWithFormat:@"%ld",self.count /38/7];
            cell.allCalorieNumber.text = [NSString stringWithFormat:@"%ld",self.count/38];
            return cell;
        }else if (indexPath.row == 2){
            weightCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"weightCollectionViewCell" forIndexPath:indexPath];
            cell.WeightLabel.text = @"最新";
            cell.BMILabel.text = @"BMI";
            NSString *newStr = [NSString stringWithFormat:@"%.1f%@",85.0,@"KG"];
            cell.WeightNumber.text = newStr;
            cell.BMINumber.text = @"27.8";
            return cell;
        }else{
            AdvancedCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AdvancedCollectionViewCell" forIndexPath:indexPath];
            cell.label.text = @"点击进入横屏模式";
            return cell;
        }

            
}

}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _titleCollectionView) {
        TitleCollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
        cell.titleLabel.textColor = [UIColor colorWithWhite:0.702 alpha:1.000];
        cell.titleLabel.font = [UIFont systemFontOfSize:13];
        
    }
}

#pragma mark collectionView点击协议方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == _titleCollectionView  ) {
        TitleCollectionViewCell *cell = (TitleCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        [self collectionView:_titleCollectionView didDeselectItemAtIndexPath:_indexPath];
        cell.titleLabel.font = [UIFont systemFontOfSize:17];
        cell.titleLabel.textColor = COLORYELLO;
//        if (indexPath.item >1 && indexPath.item < 4
//            ) {
//            _titleCollectionView.contentOffset = CGPointMake(SCREEN_WIDTH/5*(indexPath.item-2), 0);
//        }
    }
    // 联动
    [_collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}

#pragma mark 联动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSInteger index = _collectionView.contentOffset.x / WIDTH_SCREEN;
    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
    
    [self collectionView:self.titleCollectionView shouldSelectItemAtIndexPath:newIndexPath];
    [self collectionView:self.titleCollectionView didDeselectItemAtIndexPath:self.indexPath];
    [self collectionView:self.titleCollectionView didSelectItemAtIndexPath:newIndexPath];
    self.indexPath = newIndexPath;
    [self.titleCollectionView scrollToItemAtIndexPath:_indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end






























